#####
## Replication File:
##
## "The Relationship between Conspiracy Theory Beliefs and Political Violence"
##
## Published in HKS Misinformation Review
##
## Adam M. Enders, Casey Klofstad, and Joseph E. Uscinski
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(lattice)
library(psych)
library(MASS)
library(foreign)
library(gridExtra)
library(ggplot2)

# Set working directory
setwd("/Users/adamenders/Dropbox/Violence and CTs/Data and Code/Replication Materials")

################################################################################

####
## Data Prep: May 2022 Survey
####

may2022 <- read.dta("May 2022 clean.dta")

dv.names <- c("climatechange", "gayagenda", "indoctrinate", "cexaggerate",
              "cpurpose", "cantivaxx", "infertility", "antivaxx", "gmos",
              "poison", "holocaust", "birther", "deepstate", "hollywood",
              "replaceUS", "replaceEU")

dvs <- may2022[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], may2022$a_violence, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], may2022$a_violence, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], may2022$a_violence, use="pairwise.complete.obs")$conf.int[2]
}


# Proportion who believe
dv.names2 <- c("belief_climatechange", "belief_gayagenda", 
               "belief_indoctrinate", "belief_cexaggerate",
               "belief_cpurpose", "belief_cantivaxx", "belief_infertility", 
               "belief_antivaxx", "belief_gmos", "belief_poison", 
               "belief_holocaust", "belief_birther", "belief_deepstate", 
               "belief_hollywood", "belief_replaceUS", "belief_replaceEU")

dvs2 <- may2022[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}


# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- dv.names

mean(corrs$estimate)
cor.test(may2022$conthink, may2022$a_violence)

cor.test(corrs$estimate, corrs$prop)


# Export
write.csv(corrs, "Violence Corrs, May 2022.csv")

################################################################################

####
## Data Prep: May 2021 Survey
####

may2021 <- read.dta("May 2021 clean.dta")

dv.names <- c("aids1", "clintonnuke", "rothschilds",
              "epstein", "onepercent", 
              "tracking", "repsteal", "billgates", "fda", 
              "lightbulbs", "truther", "cellphone2", "soros", 
              "cellphone")

dvs <- may2021[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], may2021$violence, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], may2021$violence, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], may2021$violence, use="pairwise.complete.obs")$conf.int[2]
}


# Proportion who believe
dv.names2 <- c("belief_aids1", "belief_clintonnuke", "belief_rothschilds",
              "belief_epstein", "belief_onepercent", "belief_tracking", 
              "belief_repsteal", "belief_billgates", "belief_fda", 
              "belief_lightbulbs", "belief_truther", 
              "belief_cellphone2", "belief_soros", 
              "belief_cellphone")

dvs2 <- may2021[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}

# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- dv.names

mean(corrs$estimate)
cor.test(may2021$conthink, may2021$violence)

cor.test(corrs$estimate, corrs$prop)

# Export
write.csv(corrs, "Violence Corrs, May 2021.csv")

################################################################################

####
## Data Prep: October 2020 Survey
####

oct2020 <- read.dta("October 2020 clean.dta")

dv.names <- c("postoffice", "mailin", 
              "believer", "satanic", "battle", "falseflag", "infecttrump",
              "faking", "arrested", "covercovid")

dvs <- oct2020[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], oct2020$violence, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], oct2020$violence, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], oct2020$violence, use="pairwise.complete.obs")$conf.int[2]
}


# Proportion who believe
dv.names2 <- c("belief_postoffice", "belief_mailin",
               "belief_believer", 
               "belief_satanic", "belief_battle", "belief_falseflag", 
               "belief_infecttrump", "belief_faking", "belief_arrested", 
               "belief_covercovid")

dvs2 <- oct2020[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}


# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- dv.names

mean(corrs$estimate)
cor.test(oct2020$conthink, oct2020$violence)

cor.test(corrs$estimate, corrs$prop)

# Export
write.csv(corrs, "Violence Corrs, October 2020.csv")

################################################################################

####
## Data Prep: March 2020 Survey
####

mar2020 <- read.dta("March 2020 clean.dta")

dv.names <- c("jfk", "aliens", "smallgroup", "trumpasset")

dvs <- mar2020[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], mar2020$violence, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], mar2020$violence, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], mar2020$violence, use="pairwise.complete.obs")$conf.int[2]
}

# Proportion who believe
dv.names2 <- c("belief_jfk", "belief_aliens", "belief_smallgroup", 
               "belief_trumpasset")

dvs2 <- mar2020[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}


# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- dv.names

mean(corrs$estimate)
cor.test(mar2020$conthink, mar2020$violence)

cor.test(corrs$estimate, corrs$prop)


# Export
write.csv(corrs, "Violence Corrs, March 2020.csv")

################################################################################

####
## Figure 1
####

## Combine correlations from "Violence Corrs, May 2022.csv", 
## "Violence Corrs, May 2021.csv", "Violence Corrs, October 2020.csv", and
## "Violence Corrs, March 2020.csv" into "Combined Corrs.csv".

fig2 <- read.csv("Combined Corrs.csv")
fig2 <- fig2[,-1]

# Descriptives
mean(fig2$estimate)

cor.test(fig2$estimate, fig2$prop)

# Plot
fig2$var <- reorder(as.factor(fig2$var), fig2$estimate)

tiff("figure1.tiff", width = 7, height = 7, units = "in", res=300, compression = 'lzw')
dotplot(var ~ estimate,
        data = fig2,
        aspect = 1.5,
        xlab = "",
        xlim = c(-0.05, 0.6),
        #par.settings = list(clip = list(panel = FALSE)), 
        key = list(columns=1, points=list(pch=c(16,1)), 
                   text=list(text=c("Correlation Coefficient", 
                                    "Proportion Believe"))),
        panel = function(x, y, subscripts, ...){
          panel.dotplot(x, y, col="black", lty = 3, pch = 16)
          panel.abline(v = 0, col = "dark grey")
          panel.segments(fig2$lower, fig2$var,
                         fig2$upper, fig2$var, col = "black")
          panel.xyplot(fig2$prop, fig2$var, col="black", pch = 1)
        }
)
dev.off()

################################################################################

####
## Figure 2
####

dv.names <- c("climatechange", "gayagenda", "indoctrinate", "cexaggerate",
              "cpurpose", "cantivaxx", "infertility", "antivaxx", "gmos",
              "poison", "holocaust", "birther", "deepstate", "hollywood",
              "replaceUS", "replaceEU")

dvs <- may2022[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], may2022$b_violence, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], may2022$b_violence, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], may2022$b_violence, use="pairwise.complete.obs")$conf.int[2]
}


# Proportion who believe
dv.names2 <- c("belief_climatechange", "belief_gayagenda", 
               "belief_indoctrinate", "belief_cexaggerate",
               "belief_cpurpose", "belief_cantivaxx", "belief_infertility", 
               "belief_antivaxx", "belief_gmos", "belief_poison", 
               "belief_holocaust", "belief_birther", "belief_deepstate", 
               "belief_hollywood", "belief_replaceUS", "belief_replaceEU")

dvs2 <- may2022[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}


# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- c("Climate change is a hoax",
               "There is a gay agenda targeted at kids",
               "Schools indoctrinate kids into gay lifestyles",
               "COVID exaggerated to hurt Trump",
               "COVID created/spread on purpose",
               "COVID used to force dangerous vaccine",
               "COVID vaccine causes infertility",
               "Dangers of vaccines hidden by establishment",
               "Dangers of GMOs being hidden",
               "Corporations secretly poison our food",
               "Holocaust deaths exaggerated",
               "Obama faked birth certificate",
               "There is a deep state working in secret",
               "Elites engaged in sex trafficking",
               "Whites being replaced in Europe",
               "Whites replaced in US by foreign labor")

mean(corrs$estimate)
cor.test(may2022$conthink, may2022$b_violence)

cor.test(corrs$estimate, corrs$prop)

total <- rbind(fig2, corrs)

# Dotplot
corrs$var <- reorder(as.factor(corrs$var), corrs$estimate)

tiff("figure2.tiff", width = 7, height = 6, units = "in", res=300, compression = 'lzw')
dotplot(var ~ estimate,
        data = corrs,
        aspect = 1.5,
        xlab = "",
        xlim = c(-0.05, 0.60),
        #par.settings = list(clip = list(panel = FALSE)), 
        key = list(columns=1, points=list(pch=c(16,1)), 
                   text=list(text=c("Correlation Coefficient", 
                                    "Proportion Believe"))),
        
        panel = function(x, y, subscripts, ...){
          panel.dotplot(x, y, col="black", lty = 3, pch = 16)
          panel.abline(v = 0, col = "dark grey")
          panel.segments(corrs$lower, corrs$var,
                         corrs$upper, corrs$var, col = "black")
          panel.xyplot(corrs$prop, corrs$var, col="black", pch = 1)
        }
)
dev.off()

################################################################################

####
## Figure 3
####

dv.names <- c("climatechange", "gayagenda", "indoctrinate", "cexaggerate",
              "cpurpose", "cantivaxx", "infertility", "antivaxx", "gmos",
              "poison", "holocaust", "birther", "deepstate", "hollywood",
              "replaceUS", "replaceEU")

dvs <- may2022[,dv.names]

results <- matrix(data = NA, nrow = ncol(dvs), ncol = 5)
rownames(results) <- colnames(dvs)
colnames(results) <- c("estimate", "lower", "upper", "prop", "var")

for (i in 1:ncol(dvs)) {
  results[i,1] <- cor.test(dvs[,i], may2022$conflict, use="pairwise.complete.obs")$estimate
  results[i,2] <- cor.test(dvs[,i], may2022$conflict, use="pairwise.complete.obs")$conf.int[1]
  results[i,3] <- cor.test(dvs[,i], may2022$conflict, use="pairwise.complete.obs")$conf.int[2]
}


# Proportion who believe
dv.names2 <- c("belief_climatechange", "belief_gayagenda", 
               "belief_indoctrinate", "belief_cexaggerate",
               "belief_cpurpose", "belief_cantivaxx", "belief_infertility", 
               "belief_antivaxx", "belief_gmos", "belief_poison", 
               "belief_holocaust", "belief_birther", "belief_deepstate", 
               "belief_hollywood", "belief_replaceUS", "belief_replaceEU")

dvs2 <- may2022[,dv.names2]

for (i in 1:ncol(dvs2)) {
  results[i,4] <- mean(dvs2[,i])
}


# Clean up and descriptives
corrs <- as.data.frame(results)
corrs$var <- c("Climate change is a hoax",
               "There is a gay agenda targeted at kids",
               "Schools indoctrinate kids into gay lifestyles",
               "COVID exaggerated to hurt Trump",
               "COVID created/spread on purpose",
               "COVID used to force dangerous vaccine",
               "COVID vaccine causes infertility",
               "Dangers of vaccines hidden by establishment",
               "Dangers of GMOs being hidden",
               "Corporations secretly poison our food",
               "Holocaust deaths exaggerated",
               "Obama faked birth certificate",
               "There is a deep state working in secret",
               "Elites engaged in sex trafficking",
               "Whites being replaced in Europe",
               "Whites replaced in US by foreign labor")

mean(corrs$estimate)
cor.test(may2022$conthink, may2022$conflict)

cor.test(corrs$estimate, corrs$prop)

total <- rbind(total, corrs)
cor.test(total$estimate, total$prop)

# Dotplot
corrs$var <- reorder(as.factor(corrs$var), corrs$estimate)

tiff("figure3.tiff", width = 7, height = 6, units = "in", res=300, compression = 'lzw')
dotplot(var ~ estimate,
        data = corrs,
        aspect = 1.5,
        xlab = "",
        xlim = c(-0.05, 0.60),
        #par.settings = list(clip = list(panel = FALSE)), 
        key = list(columns=1, points=list(pch=c(16,1)), 
                   text=list(text=c("Correlation Coefficient", 
                                    "Proportion Believe"))),
        
        panel = function(x, y, subscripts, ...){
          panel.dotplot(x, y, col="black", lty = 3, pch = 16)
          panel.abline(v = 0, col = "dark grey")          
          panel.segments(corrs$lower, corrs$var,
                         corrs$upper, corrs$var, col = "black")
          panel.xyplot(corrs$prop, corrs$var, col="black", pch = 1)
        }
)
dev.off()

################################################################################

####
## Figure 4
####

acts <- read.csv("ACTS Corrs.csv")

tiff("figure4.tiff", width = 5, height = 4, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ scale,
       data = acts,
       aspect = 0.75,
       ylim = c(0, 0.6),
       xlab = "",
       ylab = "Correlation Coefficient",
       scales = list(x = list(at = c(1, 5, 7, 8, 9, 10, 11), 
                              labels = c("2012", "2016", "2018", "2019", 
                                         "2020", "2021", "2022"))),
       panel = function(x, y, ...){
         panel.xyplot(x, y, col = "black", pch = 16)
         panel.segments(x, acts$lower, x, acts$upper)
         panel.loess(x, y, col = "dark grey")
       }
       )
dev.off()

